﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Itop.YHPC.Model;
using Itop.YHPC.controller;
using System.Text;

namespace Itop.YHPC.search
{
    public class ExportStatistics
    {
        public IList<MyStatistics> GetList(string kssj, string jssj, string dw)
        {
            IList<yhpcMain> list = new List<yhpcMain>();
            StringBuilder sbulder = new StringBuilder();
            sbulder.Append(" where 1=1 ");
            if (kssj != "100")
            {
                if (!string.IsNullOrEmpty(dw) && dw != "全部")
                    sbulder.Append(" and yhdw='" + dw + "'");
                if (kssj != "null" && kssj!="")
                    sbulder.Append(" and fxrq>='" + kssj + "'");
                if (jssj != "null" && jssj != "")
                    sbulder.Append(" and fxrq<='" + jssj + "'");

            }
            list = workflowController.GetMainList(sbulder.ToString() + " order by yhdw");
            string temp = "";
            int hj_s = 0, hj_x = 0, za_s = 0, za_x = 0, yb_s = 0, yb_x = 0, zd_s = 0, zd_x = 0;
            IList<MyStatistics> rstList = new List<MyStatistics>();
            MyStatistics stat = null;
            foreach (yhpcMain main in list)
            {
                if (temp != main.yhdw)
                {
                    temp=main.yhdw;
                    if (stat != null)
                    {
                        stat.hj_hjyhs = hj_s.ToString();
                        stat.hj_xqyhs = hj_x.ToString();
                        stat.hj_xql = hj_s!=0?Math.Round(double.Parse(hj_x.ToString()) / hj_s, 2).ToString():"0";

                        stat.za_hjyhs = za_s.ToString();
                        stat.za_xqyhs = za_x.ToString();
                        stat.za_xql = za_s!=0?Math.Round(double.Parse(za_x.ToString()) / za_s, 2).ToString():"0";

                        stat.yb_hjyhs = yb_s.ToString();
                        stat.yb_xqyhs = yb_x.ToString();
                        stat.yb_xql = yb_s!=0?Math.Round(double.Parse(yb_x.ToString()) / yb_s, 2).ToString():"0";

                        stat.zd_hjyhs = zd_s.ToString();
                        stat.zd_xqyhs = zd_x.ToString();
                        stat.zd_xql = zd_s!=0?Math.Round(double.Parse(zd_x.ToString()) / zd_s, 2).ToString():"0";
                        rstList.Add(stat);
                    }
                    stat = new MyStatistics();
                    stat.dw = main.yhdw;
                    stat.rq = (kssj=="null"?"":kssj) + " - " + (jssj=="null"?"":jssj);
                    
                    hj_s = 0; hj_x = 0; za_s = 0; za_x = 0; yb_s = 0; yb_x = 0; zd_s = 0; zd_x = 0;
                }
                if (main.pgdj == "障碍隐患" || main.ypgdj == "障碍隐患")
                {
                    hj_s++; za_s++;
                    if (main.yssfxc == "是")
                    { za_x++; hj_x++; }
                }
                else if (main.pgdj == "一般事故隐患")
                {
                    hj_s++; yb_s++;
                    if (main.yssfxc == "是")
                    { yb_x++; hj_x++; }
                }
                else if (main.pgdj == "重大事故隐患")
                {
                    hj_s++; zd_s++;
                    if (main.yssfxc == "是")
                    { zd_x++; hj_x++; }
                }
                if (main.id == list[list.Count - 1].id)
                {
                    stat.hj_hjyhs = hj_s.ToString();
                    stat.hj_xqyhs = hj_x.ToString();
                    stat.hj_xql = hj_s != 0 ? Math.Round(double.Parse(hj_x.ToString()) / hj_s, 2).ToString() : "0";

                    stat.za_hjyhs = za_s.ToString();
                    stat.za_xqyhs = za_x.ToString();
                    stat.za_xql = za_s != 0 ? Math.Round(double.Parse(za_x.ToString()) / za_s, 2).ToString() : "0";

                    stat.yb_hjyhs = yb_s.ToString();
                    stat.yb_xqyhs = yb_x.ToString();
                    stat.yb_xql = yb_s != 0 ? Math.Round(double.Parse(yb_x.ToString()) / yb_s, 2).ToString() : "0";

                    stat.zd_hjyhs = zd_s.ToString();
                    stat.zd_xqyhs = zd_x.ToString();
                    stat.zd_xql = zd_s != 0 ? Math.Round(double.Parse(zd_x.ToString()) / zd_s, 2).ToString() : "0";
                    rstList.Add(stat);
                }
            }
            return rstList;
        }
    }
}

public class MyStatistics
{
    private string _dw = "";
    private string _rq = "";
    private string _hj_hjyhs = "";
    private string _hj_xqyhs = "";
    private string _hj_xql = "";
    private string _za_hjyhs = "";
    private string _za_xqyhs = "";
    private string _za_xql = "";
    private string _yb_hjyhs = "";
    private string _yb_xqyhs = "";
    private string _yb_xql = "";
    private string _zd_hjyhs = "";
    private string _zd_xqyhs = "";
    private string _zd_xql = "";

    public string dw
    {
        get { return _dw; }
        set { _dw = value; }
    }
    public string rq
    {
        get { return _rq; }
        set { _rq = value; }
    }
    public string hj_hjyhs
    {
        get { return _hj_hjyhs; }
        set { _hj_hjyhs = value; }
    }
    public string hj_xqyhs
    {
        get { return _hj_xqyhs; }
        set { _hj_xqyhs = value; }
    }
    public string hj_xql
    {
        get { return _hj_xql; }
        set { _hj_xql = value; }
    }
    public string za_hjyhs
    {
        get { return _za_hjyhs; }
        set { _za_hjyhs = value; }
    }
    public string za_xqyhs
    {
        get { return _za_xqyhs; }
        set { _za_xqyhs = value; }
    }
    public string za_xql
    {
        get { return _za_xql; }
        set { _za_xql = value; }
    }
    public string yb_hjyhs
    {
        get { return _yb_hjyhs; }
        set { _yb_hjyhs = value; }
    }
    public string yb_xqyhs
    {
        get { return _yb_xqyhs; }
        set { _yb_xqyhs = value; }
    }
    public string yb_xql
    {
        get { return _yb_xql; }
        set { _yb_xql = value; }
    }
    public string zd_hjyhs
    {
        get { return _zd_hjyhs; }
        set { _zd_hjyhs = value; }
    }
    public string zd_xqyhs
    {
        get { return _zd_xqyhs; }
        set { _zd_xqyhs = value; }
    }
    public string zd_xql
    {
        get { return _zd_xql; }
        set { _zd_xql = value; }
    }
}